# -*- coding: UTF-8 -*-

"""
# Filename: is_numbers.py
# author by:  Sylvanas Windrunner
"""

from random import sample
print('*'*30+'EXAMPLE 1:'+'*'*50)
# 利用range()函数取一个范围内的随机值，利用sample()函数获得随机数中的20个
grades_list = sample(range(50, 101), 20)
# 利用sort()函数正向排序
grades_list.sort()
print('20个同学随机成绩正向排序为：', grades_list)


# *middle变量可以取得成绩列表中除去第一个和最后一个的所有成绩
def drop_first_last(grades):
    first, *middle, last = grades
    return middle


# 计算平均值
def avg_grades(score_list):
    return sum(score_list)/len(score_list)


middle_list = drop_first_last(grades_list)
print('去掉最高分和最低分后的成绩为：{}'.format(middle_list))
# {0:.2f}可以在format函数中精确平均成绩为小数点后两位
avg_score = avg_grades(middle_list)
print('20个同学中去掉一个高分:{1},去掉一个最低分:{0}平均成绩为{2:.2f}'.format(grades_list[0], grades_list[-1], avg_score))

print('*'*30+'EXAMPLE 2:'+'*'*50)

"""
一个用户记录列表，每条记录包含一个名字，一个邮件，还有不确定数量的电话号码
"""
record = ('Dave', 'dave@example.com', '029-88386161', '029-88381313')
name, email, *phone_numbers = record
print('姓名:{}\n年龄：{}\n电话号码:{}'.format(name, email, phone_numbers))

print('*'*30+'EXAMPLE 3:'+'*'*50)

# 魏家凉皮库存[品名, 数量, 单价]
wj_records = [
    ('凉皮', 20, 5),
    ('凉皮', 30, 8.5),
    ('肉夹馍', '估清')
]


# 获取凉皮数量
def do_foo(x, y):
    print('凉皮:', x, y)


# 获取肉夹馍数量
def do_bar(s):
    print('肉夹馍:', s)


for tag, *args in wj_records:
    if tag == '凉皮':
        do_foo(*args)
    elif tag == '肉夹馍':
        do_bar(*args)
        
print('*'*30+'EXAMPLE 4:'+'*'*50)

import heapq
nums = sample(range(-10, 50), 10)
print('生成10个随机数：', nums)
print('其中最大的三个数为:{}'.format(heapq.nlargest(3, nums)))
print('其中最小的三个数为：{}'.format(heapq.nsmallest(3, nums)))

print('*'*30+'EXAMPLE 5:'+'*'*50)

portfolio = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'name': 'FB', 'shares': 200, 'price': 21.09},
    {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheep = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
print('cheep: {}\nexpensive: {}'.format(cheep, expensive))

print('*'*30+'EXAMPLE 6:'+'*'*50)

nums_a = [26, -6, 6, 9, 31, 27, 49, -3, 13, 14]
heaq = list(nums_a)
heapq.heapify(heaq)
print('原始列表', nums_a)
print('将最小值移动到列表头', heaq)
print('取出最小值：', heapq.heappop(heaq))
print('取出最小值：', heapq.heappop(heaq))
print('取出最小值：', heapq.heappop(heaq))
print('取出最小值：', heapq.heappop(heaq))
print('取出最小值：', heapq.heappop(heaq))
print('取出最小值：', heapq.heappop(heaq))
print('最后列表剩余值：', heaq)

print('*'*30+'EXAMPLE 7:'+'*'*50)


